package com.leozhang.portalssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leozhang.portalssm.entity.Assets;
import com.leozhang.portalssm.entity.AssetsExample;
import com.leozhang.portalssm.mapper.AssetsMapper;
import com.leozhang.portalssm.service.AssetService;
import com.leozhang.portalssm.util.ChangeChar;
import com.leozhang.portalssm.util.Result;
import org.apache.ibatis.cache.TransactionalCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class AssetServiceImpl implements AssetService {


    @Autowired
    private AssetsMapper assetsMapper;



    @Override
    public Result getListForPage(int pno, int psize, String assetTypeName, String sortField, String sortType) {
        Page<Object> p = PageHelper.startPage(pno, psize);
        AssetsExample assetsExample = new AssetsExample();
        AssetsExample.Criteria criteria = assetsExample.createCriteria();
        if(assetTypeName.trim().length()>0){
            criteria.andAssetTypeNameLike("%"+assetTypeName+"%");
        }
        if(sortField.trim().length()>0){
            assetsExample.setOrderByClause(ChangeChar.camelToUnderline(sortField,2) +""+sortType);
        }
        List<Assets> list = assetsMapper.selectByExample(assetsExample);
        return Result.end(200,list,"查询成功",p.getTotal());
    }

    @Override
    public void insertAssets(Assets assets) {
        assetsMapper.insert(assets);

    }

    @Override
    public void updateAssets(Assets assets) {
        assetsMapper.updateByPrimaryKeySelective(assets);
    }

    @Override
    public Assets selectAssetsById(Long id) {
        Assets assets = assetsMapper.selectByPrimaryKey(id);
        return assets;
    }

    @Override
    public void deleteAssetById(Long id) {
        assetsMapper.deleteByPrimaryKey(id);
    }
}
